using EasyNetQ;
using EasyNetQ.ConnectionString;
using EasyNetQ.DI;
using EasyNetQ.DI.LightInject;

// ReSharper disable once CheckNamespace
namespace LightInject;

public static class EasyNetQServiceContainerExtensions
{
    public static IServiceContainer RegisterEasyNetQ(
        this IServiceContainer serviceContainer,
        Func<IServiceResolver, ConnectionConfiguration> connectionConfigurationFactory,
        Action<IServiceRegister> registerServices
    )
    {
        var serviceRegister = new LightInjectAdapter(serviceContainer);
        RabbitHutch.RegisterBus(serviceRegister, connectionConfigurationFactory, registerServices);
        return serviceContainer;
    }

    public static IServiceContainer RegisterEasyNetQ(
        this IServiceContainer serviceContainer,
        Func<IServiceResolver, ConnectionConfiguration> connectionConfigurationFactory
    ) => serviceContainer.RegisterEasyNetQ(connectionConfigurationFactory, _ => { });

    public static IServiceContainer RegisterEasyNetQ(
        this IServiceContainer serviceContainer,
        string connectionString,
        Action<IServiceRegister> registerServices
    ) => serviceContainer.RegisterEasyNetQ(c => c.Resolve<IConnectionStringParser>().Parse(connectionString), registerServices);

    public static IServiceContainer RegisterEasyNetQ(this IServiceContainer serviceContainer, string connectionString)
        => serviceContainer.RegisterEasyNetQ(c => c.Resolve<IConnectionStringParser>().Parse(connectionString));
}
